<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Media extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	
        function Media()
	{
            parent::Controller();            
            
	}*/
	public function index()
	{    
                  
	}
        public function edit($media_id)
        {
            $this->load->model('media_model','',TRUE);            
            $res_arr = $this->media_model->get_media($media_id);  
            $data['media'] = $res_arr;                       
            $this->load->view('edit_media', $data);
        }
        public function view($media_id)
        {            
            $this->load->model('media_model','',TRUE);
            $this->load->model('profile_model','',TRUE);
            $this->load->model('comment_model','',TRUE);
            $this->load->model('ratings_model','',TRUE);
            $this->load->model('tag_model','',TRUE);
            
            $data['ratings'] = $this->ratings_model->get_rating($media_id);
            $res_arr = $this->media_model->get_media($media_id);           
            $data['media'] = $res_arr;
            $user_id = $data['media']['owner_id'];            
            $res_arr = $this->profile_model->get_profile($user_id);
            $data['user'] = $res_arr;
            $data['comments'] = $this->comment_model->get_comments($media_id);
            $data['tags'] = $this->tag_model->get_tags_by_media_id($media_id);
            $this->load->view('media', $data);            
        }
        
        public function add_vote($media_id,$rating)
        {          
            if($this->session->userdata('logged_in'))
            {        
                $this->load->model('media_model','',TRUE);
                $this->load->model('ratings_model','',TRUE);
                if($this->ratings_model->has_voted($media_id))
                {
                    redirect(site_url("media/view/".$media_id), 'location');
                }
                if($rating > 0)
                    $rating = 1;
                if($rating < 0)
                    $rating = -1;
                if ($rating != 0)
                    $this->ratings_model->save_rating($media_id,$rating);
                
            }  
            redirect(site_url("media/view/".$media_id), 'location');
        }
        
        public function add_comment()
        {            
            if($this->session->userdata('logged_in'))
            {
                $media_id = $this->input->post('media_id');
                $this->load->model('comment_model','',TRUE);
                $this->comment_model->save_comment($media_id,$this->input->post('comment'));
                
            }  
            redirect(site_url("media/view/".$media_id), 'location');
        }
        
        public function add_tags()
        {            
            if($this->session->userdata('logged_in'))
            {
                $media_id = $this->input->post('media_id');
                $tags = $this->input->post('added_tags');
                $tags = str_replace(';', ' ', $tags);
                $tags = str_replace(',', ' ', $tags);
                $arr_tags = explode(' ', $tags);
                
                $this->load->model('tag_model','',TRUE);
                $this->tag_model->save_tags($arr_tags);
                $this->tag_model->attach_tags_to_video($arr_tags,$media_id);
                
            }  
            redirect(site_url("media/view/".$media_id), 'location');
        }
        
        public function create()
        {
            $this->load->view('upload_media');
        }
        public function update()
        {
            $this->load->model('media_model','',TRUE); 
            $media_id = $this->input->post('media_id');
            $res_arr = $this->media_model->get_media($media_id); 
            $user_id = $res_arr['owner_id'];
            if($this->session->userdata('user_id') == $user_id)
            {
                $this->media_model->update_media();  
            } 
            redirect(site_url("media/view/".$media_id), 'location');
        }
        public function save()
        {
            $this->load->model('media_model','',TRUE);
            $this->media_model->save_media();  
            redirect(base_url(), 'location');  
            //echo '<iframe width="420" height="315" src="http://www.youtube.com/embed/' . $media_id . '" frameborder="0" allowfullscreen></iframe>';	
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */